package SWFData;

import java.io.*;
import java.util.StringTokenizer;

import myio.StringOps;

import Learn.clusterer;
import Learn.clusterparser;
import Data.datamanager;

public class SWFanalyzer {

	SWFdatamanager dm;
	String mapdatasource;
//	int[] layerstruc;
	int[][] clusterinfo;
	double[][] mi;
	
	private SWFanalyzer(){
		dm = null;
	}
	
	private void printhelp(){
		System.out.print("init <mrffile>" + '\n' + '\n' );
		System.out.print("Load clustering information from R output file:" + '\n');
		System.out.print("lc <clusterfile>" + '\n' + '\n' );
		System.out.print("Produce xfig maps of all clusterings:" + '\n');
		System.out.print("plotclusterings <outputfilesbase>" + '\n'+ '\n' );
		System.out.print("Plot plant distribution map:" + '\n');
		System.out.print("plotplant <plantid> <outputfile>" + '\n'+ '\n' );
		System.out.print("Compute the mutual information matrix"+ '\n' );
		System.out.print("computeMI" + '\n'+ '\n' );
		System.out.print("Get the plant with highest MI for clustering"+ '\n' );
		System.out.print("getTopMI <clusindex> <number>" + '\n'+ '\n' );
		System.out.print("Save MI matrix to arff format"+ '\n' );
		System.out.print("saveMI <outputfile>" + '\n'+ '\n' );
	}
	
	
	private void initSource(String sourcefile){
		mapdatasource = sourcefile;
		dm = new SWFdatamanager();
		dm.loadData(sourcefile);
	}

	private void loadClustering(StringTokenizer st){
		clusterinfo = clusterparser.readClusters(st.nextToken());
		System.out.println(StringOps.matrixToString(clusterinfo, "(", ")"));
	}
	
	private void plotClusterings(StringTokenizer st){
		dm.plotClusterings(clusterinfo, st.nextToken());
	}
	
	private void plotPlant(StringTokenizer st){
		int pind = Integer.parseInt(st.nextToken());
		String outf = st.nextToken();
		
		if (dm == null)
			System.out.println("use 'init <datafile>' first" );
		else{
			dm.plotPlant(mapdatasource, pind, outf);
		}
	}
	
	private void computeMI(){
		mi = datamanager.mutualinformation(dm, clusterinfo);
	}
	
	private void getTopMI(StringTokenizer st){
		int clus = Integer.parseInt(st.nextToken());
		int num = Integer.parseInt(st.nextToken());
		int idx;
		
		double[][] sortedmi = datamanager.sortMI(mi,clus);
		
		for (int i=0;i<num;i++){
			idx = (int)sortedmi[i][1];
			System.out.println( sortedmi[i][0] + " " + dm.getAttributeName(idx)+ "(" + idx +")");
		}
	}
	
	private void saveMI(StringTokenizer st){
		String outf = st.nextToken();
		datamanager.saveMI(dm, mi,outf);
	}
	
//	private void getRegionTopMI(StringTokenizer st){
//		int clus = Integer.parseInt(st.nextToken());
//		int num = Integer.parseInt(st.nextToken());
//		int idx;
//		
//		for (int i=0;i<num;i++){
//			idx = (int)mi[clus][i][1];
//			System.out.println( mi[clus][i][0] + " " + dm.getName(idx)+ "(" + idx +")");
//		}
//	}
	
	public static void main(String[] args) {
		
		SWFanalyzer anl = new SWFanalyzer();
		
		String commandline = "";
		String command;
		BufferedReader br =
				new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
	
		try
		{

			while( !commandline.equals("exit") )
			{
				System.out.print(">");
				commandline=br.readLine();
				st = new StringTokenizer(commandline, " ", false);
				
				command = st.nextToken();
				if (command.equals("?") || command.equals("help")){
					anl.printhelp();
				}
				if (command.equals("init")){
					anl.initSource(st.nextToken());
				}
				
				/* load a clustering as output by r*/
				if (command.equals("lc")){
					anl.loadClustering(st);
				}
				
				/* make xfig plots of all clusterings */
				if (command.equals("plotclusterings")){
					anl.plotClusterings(st);
				}
				
				if (command.equals("plotplant")){
					anl.plotPlant(st);
				}
				
				if (command.equals("computeMI")){
					anl.computeMI();
				}
				
				if (command.equals("getTopMI")){
					anl.getTopMI(st);
				}
				if (command.equals("saveMI")){
					anl.saveMI(st);
				}
			}

			br.close();                    

		}
		catch(Exception e)
		{
			System.out.println(e);
		}

	}

}
